var Personnel = function() {

    var datalist_url = $('.datalist_url').val();
    var edit_url = $('.edit_url').val();
    var delete_url = $('.delete_url').val();
    var get_statistics_url = $('.get_statistics_url').val();
    var handle_url = $('.handle_url').val();
    var discuss_url = $('.discuss_url').val();
    var evaluate_url = $('.evaluate_url').val();

    var initTable = function() {
        var table = $('#data-table');

        if(table.length > 0){
            table.dataTable({
                "processing": true,
                serverSide: true,// 开启服务器模式
                "ajax": {
                    url: datalist_url,
                },
                "autoWidth": false,
                "columns": [
                    {"data": "title"},
                    {
                        "data": "typeId",
                        "render": function(data, type, row, meta) {
                            return data == 1 ? '内部工作' : '信访工作';
                        }
                    },
                    {"data": "name"},
                    {"data": "createDate"},
                    {
                        "data": "schedule",
                        "render": function(data, type, row, meta) {
                            if(data == 1){
                                return '未开始'
                            }else if(data == 2){
                                return '进行中'
                            }else {
                                return '已完成'
                            }
                        }
                    },
                    {
                        "width": "20%",
                        "render": function(data, type, row, meta) {
                            $item = '';
                            if(row.showhandle == 1){
                                $item += '<a href="'+handle_url+'?id='+row.id+'" type="button" class="btn btn-success">办理</a>' +
                                    '<a href="'+discuss_url+'?id='+row.id+'" type="button" class="btn btn-success">交流</a>';
                            }
                            if(row.showedit == 1){
                                $item += '<a href="'+edit_url+'?id='+row.id+'" type="button" class="btn btn-success">编辑</a>' +
                                    '<a href="'+evaluate_url+'?id='+row.id+'" type="button" class="btn btn-success">评价</a>' +
                                    '<button type="button" class="btn btn-danger delete-btn">删除</button>';
                            }
                            return $item;
                        }
                    }
                ],
                "language": {
                    url: '/public/static/admin/pages/datatable_cn.json'
                },
                "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
                "pageLength": 10,
                "scrollX":"",
                "destroy": false,
                "stateSave": true,
                "stateSaveParams": function (settings, data) {
                    var search = data.search.search;
                    if(search){
                        var arr = $.parseJSON(search);
                        for(var key in arr){
                            $("#searchForm input[name='"+key+"']").val(arr[key]);
                            $("#searchForm select[name='"+key+"']").val(arr[key]);
                        }
                    }
                },
                "order": [
                    [3, "desc"]
                ],
                "columnDefs": [{
                    "orderable": false,
                    "targets": [0,1,2,4]
                }],
                "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'relative'<'col-md-5'i><'col-md-7'>lp>",
                "fnInitComplete": function() {
                    var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
                }
            });
        }


        // 删除
        table.on('click', '.delete-btn', function(event) {
            // 操作行对象
            var dataArr = table.DataTable().rows($(this).parents("tr")).data();
            var id = dataArr[0].id;
            var _this = $(this);

            var text = '是否要删除这条数据？';
            var title = '请确认';
            var confirmBtn = '确定';
            var cancelBtn = '取消';
            event.preventDefault();

            layer.confirm(
                text,
                {
                    title: title,
                    btn: [confirmBtn, cancelBtn]
                },
                function(index){
                    layer.close(index);
                    var url = delete_url;
                    var data = {'id':id};
                    $.ajax({
                        url: url,
                        type: 'POST',
                        data: data,
                        dataType : 'json',
                        success: function (data) {
                            layer.msg(data.msg);
                            if(data.code == 1){
                                _this.parents('tr').remove();
                            }
                        }
                    });
                }
            );
        });
    };

    var init_datepicker = function () {
        if($('.s_complete_time').length > 0){
            $('.s_complete_time').datepicker({
                language:"cn",
                format: 'yyyy-mm-dd',
                todayBtn : "linked",
                autoclose : true,
                todayHighlight : true,
            });
        }

        if($('.e_complete_time').length > 0){
            $('.e_complete_time').datepicker({
                language:"cn",
                format: 'yyyy-mm-dd',
                todayBtn : "linked",
                autoclose : true,
                todayHighlight : true,
            });
        }

    };

    var tableSearch = function(table, params) {
        table.DataTable().search(JSON.stringify(params)).draw();
    };


    return {
        init: function() {
            //initTable();
            init_datepicker();

            this.onEvent();
        },

        onEvent : function(){

            $('#doSearch').on('click', function(event){
                var table = $('#data-table');
                if(!table.is(':visible')){
                    table = $('#checkdata-table');
                }

                var param = $('#searchForm').serializeObject();
                console.log(param);
                if(param.s_complete_time != '' && param.e_complete_time == ''){

                    layer.msg('请选择结束时间');
                    return;
                }
                if(param.s_complete_time == '' && param.e_complete_time != ''){

                    layer.msg('请选择开始时间');
                    return;
                }
                if(param.s_complete_time != '' && param.e_complete_time != ''){
                    if(param.s_complete_time > param.e_complete_time){
                        layer.msg('开始时间不能大于结束时间');

                        return;
                    }
                }


                $.ajax({
                    url : get_statistics_url,
                    type : 'post',
                    dataType : 'json',
                    contentType : "application/json; charset=utf-8",
                    data : JSON.stringify(param),
                }).done(function(data){
                    console.log(data);
                    if (data.status == 1) {
                        $('#work_all').text(data.data.work_all)
                        $('#work_internal').text(data.data.work_internal)
                        $('#work_visit').text(data.data.work_visit)
                        $('#complete_all').text(data.data.complete_all)
                        $('#hangintheairnum').text(data.data.hangintheairnum)
                        $('#complete_all_overdue').text(data.data.complete_all_overdue)
                        $('#internal_satisfaction').text(data.data.internal_satisfaction)
                        $('#internal_basic').text(data.data.internal_basic)
                        $('#internal_no').text(data.data.internal_no)
                        $('#visit_satisfaction').text(data.data.visit_satisfaction)
                        $('#visit_basic').text(data.data.visit_basic)
                        $('#visit_no').text(data.data.visit_no)

                    } else {  // 错误
                        layer.msg(data.message);
                    }
                });
            });


        }
    };

}();

$(function() {
    Personnel.init();
});

function getPensonList(e, type) {
    $('.'+type).html('');
    var name = '请选择';
    if(e.value != 0) {
        $.ajax({
            url : getPensonListurl,
            type : 'post',
            dataType : 'json',
            contentType : "application/json; charset=utf-8",
            data : JSON.stringify({'id':e.value}),
        }).done(function(data) {
            var $item = "<option value='0'>"+name+"</option>";
            for (var i in data){
                $item += "<option  value='"+i+"' '>"+data[i]+"</option>";
            }
            $('.'+type).append($item);
        });
    } else {
        $('.'+type).append("<option value='0'>"+name+"</option>");
    }
}

function showPeson(e) {
    if(e.value != 0){
        var flag = true;
        $.ajax({
            url : getPersonurl,
            type : 'post',
            dataType : 'json',
            contentType : "application/json; charset=utf-8",
            data : JSON.stringify({'id':e.value}),
        }).done(function(data) {

            $(".personIds").each(function () {
                if(e.value == $(this).val()){
                    flag = false;
                }
            })
            if(flag){
                var $item = '';
                $item += '<div style="position: relative;float: left;margin:20px 20px 20px 0">';
                $item += '<input type="hidden" class="personIds" name="personIds" value="'+data.id+'">';
                $item += '<img class="exist-image" src="'+data.p_imgUrl+'" alt="" />';
                $item += '<img class="del" src="/public/static/pages/image/del.png" alt="">';
                $item += '<span style="position: absolute;top:102px;left:34px;">'+data.p_name+'</span>';
                $item += '</div>';
                $('#personlist').append($item);
            }

        });
    }
}
